/**
 * Copyright (C) 2025 pony working team
 * SPDX-License-Identifier: MulanPSL-2.0
 */

/* includes (local) ----------------------------------------------------------*/
#include "k_errno.h"
#include "k_task.h"
/* includes (standard library, system) ---------------------------------------*/
/* includes (other library) --------------------------------------------------*/
/* includes (project) --------------------------------------------------------*/
#include <x_common.h>
#include <x_it.h>
#include <x_bug.h>

#ifdef CONFIG_SYSCALL
#include <x_syscall.h>
#endif

/* defines -------------------------------------------------------------------*/
/* typedefs ------------------------------------------------------------------*/
/* macro ---------------------------------------------------------------------*/
/* variables (extern) --------------------------------------------------------*/
/* variables (global) --------------------------------------------------------*/
/* variables (local) ---------------------------------------------------------*/

static uint32_t __sys_error;

/* inlines -------------------------------------------------------------------*/
/* functions -----------------------------------------------------------------*/

/**
 * errno_get - errno address get routine
 *
 * return: 0 on success, negative value on error
 */
uint32_t *errno_get(void)
{
    if (it_context() || current == NULL) {
        return &__sys_error;
    } else {
        return task_errno_get(current);
    }
}

#ifdef CONFIG_SYSCALL
static const uintptr_t syscall_entries_errno[] = {
    (uintptr_t)errno_get,
};

const struct syscall_table syscall_table_errno = {
    ARRAY_SIZE(syscall_entries_errno),
    syscall_entries_errno,
};
#endif
